import { z } from 'zod'
import type { Request } from 'express'
import { todoService } from '@server/services/todo-service'
import { ApiResponse } from '@data/api'
import { ParameterError } from '@server/error/errors'

// 导出参数验证 schema
export const bodySchema = z.object({
  items: z.array(
    z.object({
      id: z.string(),
      sortOrder: z.number(),
    })
  ),
})

export default async function sortTodos(request: Request, data: z.infer<typeof bodySchema>) {
  try {
    await todoService.updateSortOrder(data.items)

    const response: ApiResponse = {
      success: true,
      message: 'Todo sort order updated successfully',
    }

    return response
  } catch (error) {
    throw new ParameterError(error instanceof Error ? error.message : 'Failed to update sort order')
  }
}
